{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "Chapter 07\n",
    "# 笛卡儿积\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "column1 = [1, 2, 3, 4]\n",
    "column2 = ['a', 'b', 'c']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T11:03:11.712928Z",
     "end_time": "2024-07-03T11:03:11.717036Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1, 'a'), (1, 'b'), (1, 'c'), (2, 'a'), (2, 'b'), (2, 'c'), (3, 'a'), (3, 'b'), (3, 'c'), (4, 'a'), (4, 'b'), (4, 'c')]\n"
     ]
    }
   ],
   "source": [
    "cartesian_product = [(x, y) for x in column1 for y in column2]\n",
    "print(cartesian_product)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T11:03:20.406732Z",
     "end_time": "2024-07-03T11:03:20.412774Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1, 'a'), (2, 'a'), (3, 'a'), (4, 'a')]\n",
      "[(1, 'b'), (2, 'b'), (3, 'b'), (4, 'b')]\n",
      "[(1, 'c'), (2, 'c'), (3, 'c'), (4, 'c')]\n"
     ]
    }
   ],
   "source": [
    "cartesian_product = [[(x, y) for x in column1] for y in column2]\n",
    "for prod_idx in cartesian_product:\n",
    "    print(prod_idx)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T11:03:31.310187Z",
     "end_time": "2024-07-03T11:03:31.361857Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1, 'a'), (1, 'b'), (1, 'c'), (2, 'a'), (2, 'b'), (2, 'c'), (3, 'a'), (3, 'b'), (3, 'c'), (4, 'a'), (4, 'b'), (4, 'c')]\n"
     ]
    }
   ],
   "source": [
    "from itertools import product\n",
    "\n",
    "cartesian_product = list(product(column1, column2))\n",
    "print(cartesian_product)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T11:03:49.894261Z",
     "end_time": "2024-07-03T11:03:49.902799Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
